package com.hytc.mymall.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.hytc.mymall.R;

/**
 * Created by Administrator on 2016/12/2.
 */
public class CircleImageView extends ImageView
{
    private static final Bitmap.Config BITMAP_CONFIG;
    private static final int COLORDRAWABLE_DIMENSION = 1;
    private static final int DEFAULT_BORDER_COLOR = -16777216;
    private static final int DEFAULT_BORDER_WIDTH = 0;
    private static final ScaleType SCALE_TYPE;
    private Bitmap mBitmap;
    private int mBitmapHeight;
    private final Paint mBitmapPaint;
    private BitmapShader mBitmapShader;
    private int mBitmapWidth;
    private int mBorderColor;
    private final Paint mBorderPaint;
    private float mBorderRadius;
    private final RectF mBorderRect;
    private int mBorderWidth;
    private float mDrawableRadius;
    private final RectF mDrawableRect;
    private boolean mReady;
    private boolean mSetupPending;
    private final Matrix mShaderMatrix;

    static {
        SCALE_TYPE = ScaleType.CENTER_CROP;
        BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
    }

    public CircleImageView(final Context context) {
        super(context);
        this.mDrawableRect = new RectF();
        this.mBorderRect = new RectF();
        this.mShaderMatrix = new Matrix();
        this.mBitmapPaint = new Paint();
        this.mBorderPaint = new Paint();
        this.mBorderColor = -16777216;
        this.mBorderWidth = 0;
        this.init();
    }

    public CircleImageView(final Context context, final AttributeSet set) {
        this(context, set, 0);
    }

    public CircleImageView(final Context context, final AttributeSet set, final int n) {
        super(context, set, n);
        this.mDrawableRect = new RectF();
        this.mBorderRect = new RectF();
        this.mShaderMatrix = new Matrix();
        this.mBitmapPaint = new Paint();
        this.mBorderPaint = new Paint();
        this.mBorderColor = -16777216;
        this.mBorderWidth = 0;
        final TypedArray obtainStyledAttributes = context.obtainStyledAttributes(set, R.styleable.CircleImageView, n, 0);
        this.mBorderWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.CircleImageView_border_width, 0);
        this.mBorderColor = obtainStyledAttributes.getColor(R.styleable.CircleImageView_border_color, Color.BLACK);
        obtainStyledAttributes.recycle();
        this.init();
    }

    private Bitmap getBitmapFromDrawable(final Drawable drawable) {
        if (drawable == null) {
            return null;
        }
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable)drawable).getBitmap();
        }
        try {
            Bitmap bitmap;
            if (drawable instanceof ColorDrawable) {
                bitmap = Bitmap.createBitmap(1, 1, CircleImageView.BITMAP_CONFIG);
            }
            else {
                bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), CircleImageView.BITMAP_CONFIG);
            }
            final Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
            return bitmap;
        }
        catch (OutOfMemoryError outOfMemoryError) {
            return null;
        }
    }

    private void init() {
        super.setScaleType(CircleImageView.SCALE_TYPE);
        this.mReady = true;
        if (this.mSetupPending) {
            this.setup();
            this.mSetupPending = false;
        }
    }

    private void setup() {
        if (!this.mReady) {
            this.mSetupPending = true;
        }
        else if (this.mBitmap != null) {
            this.mBitmapShader = new BitmapShader(this.mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            this.mBitmapPaint.setAntiAlias(true);
            this.mBitmapPaint.setShader((Shader)this.mBitmapShader);
            this.mBorderPaint.setStyle(Paint.Style.STROKE);
            this.mBorderPaint.setAntiAlias(true);
            this.mBorderPaint.setColor(this.mBorderColor);
            this.mBorderPaint.setStrokeWidth((float)this.mBorderWidth);
            this.mBitmapHeight = this.mBitmap.getHeight();
            this.mBitmapWidth = this.mBitmap.getWidth();
            this.mBorderRect.set(0.0f, 0.0f, (float)this.getWidth(), (float)this.getHeight());
            this.mBorderRadius = Math.min((this.mBorderRect.height() - this.mBorderWidth) / 2.0f, (this.mBorderRect.width() - this.mBorderWidth) / 2.0f);
            this.mDrawableRect.set((float)this.mBorderWidth, (float)this.mBorderWidth, this.mBorderRect.width() - this.mBorderWidth, this.mBorderRect.height() - this.mBorderWidth);
            this.mDrawableRadius = Math.min(this.mDrawableRect.height() / 2.0f, this.mDrawableRect.width() / 2.0f);
            this.updateShaderMatrix();
            this.invalidate();
        }
    }

    private void updateShaderMatrix() {
        float n = 0.0f;
        this.mShaderMatrix.set((Matrix)null);
        float n2;
        float n3;
        if (this.mBitmapWidth * this.mDrawableRect.height() > this.mDrawableRect.width() * this.mBitmapHeight) {
            n2 = this.mDrawableRect.height() / this.mBitmapHeight;
            n3 = 0.5f * (this.mDrawableRect.width() - n2 * this.mBitmapWidth);
        }
        else {
            n2 = this.mDrawableRect.width() / this.mBitmapWidth;
            n = 0.5f * (this.mDrawableRect.height() - n2 * this.mBitmapHeight);
            n3 = 0.0f;
        }
        this.mShaderMatrix.setScale(n2, n2);
        this.mShaderMatrix.postTranslate((float)((int)(n3 + 0.5f) + this.mBorderWidth), (float)((int)(n + 0.5f) + this.mBorderWidth));
        this.mBitmapShader.setLocalMatrix(this.mShaderMatrix);
    }

    public int getBorderColor() {
        return this.mBorderColor;
    }

    public int getBorderWidth() {
        return this.mBorderWidth;
    }

    public ScaleType getScaleType() {
        return CircleImageView.SCALE_TYPE;
    }

    protected void onDraw(final Canvas canvas) {
        if (this.getDrawable() != null) {
            canvas.drawCircle((float)(this.getWidth() / 2), (float)(this.getHeight() / 2), this.mDrawableRadius, this.mBitmapPaint);
            if (this.mBorderWidth != 0) {
                canvas.drawCircle((float)(this.getWidth() / 2), (float)(this.getHeight() / 2), this.mBorderRadius, this.mBorderPaint);
            }
        }
    }

    protected void onSizeChanged(final int n, final int n2, final int n3, final int n4) {
        super.onSizeChanged(n, n2, n3, n4);
        this.setup();
    }

    public void setBorderColor(final int mBorderColor) {
        if (mBorderColor == this.mBorderColor) {
            return;
        }
        this.mBorderColor = mBorderColor;
        this.mBorderPaint.setColor(this.mBorderColor);
        this.invalidate();
    }

    public void setBorderWidth(final int mBorderWidth) {
        if (mBorderWidth == this.mBorderWidth) {
            return;
        }
        this.mBorderWidth = mBorderWidth;
        this.setup();
    }

    public void setImageBitmap(final Bitmap bitmap) {
        super.setImageBitmap(bitmap);
        this.mBitmap = bitmap;
        this.setup();
    }

    public void setImageDrawable(final Drawable imageDrawable) {
        super.setImageDrawable(imageDrawable);
        this.mBitmap = this.getBitmapFromDrawable(imageDrawable);
        this.setup();
    }

    public void setImageResource(final int imageResource) {
        super.setImageResource(imageResource);
        this.mBitmap = this.getBitmapFromDrawable(this.getDrawable());
        this.setup();
    }

    public void setImageURI(final Uri imageURI) {
        super.setImageURI(imageURI);
        this.mBitmap = this.getBitmapFromDrawable(this.getDrawable());
        this.setup();
    }

    public void setScaleType(final ScaleType imageView$ScaleType) {
        if (imageView$ScaleType != CircleImageView.SCALE_TYPE) {
            throw new IllegalArgumentException(String.format("ScaleType %s not supported.", imageView$ScaleType));
        }
    }
}
